<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');

into::basic_class('origin');
into::basic_class('cache');
into::basic_class('seo');

class web extends origin
{	
	public function init()
	{
		global $G;
		if(!$G['config']['domain'] && !$G['config']['domain_mobile']){
			die('未设置访问域名');  // 如果未设置访问域名网站将无法访问
		}
		$ht = parse_url($G['path']['site']);
		$mdn = parse_url($G['config']['domain_mobile']);
		if($G['config']['domain_mobile'] && isMobile() && $ht['host']!=$mdn['host']){
			location($G['config']['domain_mobile'], 301);
		}
		$dn = $G['config']['domain']?parse_url($G['config']['domain']):$mdn;
		if(rootDomain($ht['host']) != rootDomain($dn['host'])){
			die('当前域名非后台填写站点域名');  // 判断当前访问域名是否为所填站点域名，是则可以访问
		}
		
		if(arrExist($G['config'],'state_open')){
			if($G['path']['home']){
				echo url::upload(load::page('close'));
				die();
			}else{
				url::page404();
			}
		}
		$G['home'] = page::items_one(88888);
	}

	public function theme($name)
	{
		global $G;
		return $this->replace(load::page('html/'.$name, null, false, 'web'));
	}
	
	public function replace($html)
	{
		global $G;
		/* 网页描述判断 */
		$html = seo::replace($html);
		/* 给没有图片地址的img标签添加默认图片 */
		$html = preg_replace('/(<img [^>]*?src=)("\s*"|\'\s*\'|\s)/',"\\1\"{$G['config']['image']}\"",$html);
		/* 替换图片地址相对路径 BOSS_CMS */
		$html = url::upload($html);
		/* 网页去除标签 */
		$html = preg_replace('/<!--[\W]*?-->/','',$html);
		if(!$G['view']){
			$html = preg_replace_callback('/<[a-zA-Z0-9][^>]*?\sbosscms\=([\'"]{0,1})[\s\w]*?\\1[^>]*?>/',function($match){
				return preg_replace('/\s(?:bosscms|items|groups|group|link|feedback|consult|menu|banner|content|layers|complex|area|plugin)(?:\s*=([\'"]{0,1})([\s\w]*?)\\1){0,1}/','',$match[0]);				
			},preg_replace('/\s\sthemectrl="\w+\-\w+"\s\s/','',$html));
		}
		/* 首页替换掉地址头部带 ../ 的地址 */
		if($G['path']['home'] && !$G['path']['relative']){
			$html = preg_replace('/(=["\'\s]*)\.\.\/(\w)/',"\\1\\2",$html);
		} /* 内页当相对地址带有两个以上../时，替换掉地址头部带 ../ 的地址 */
		else if(strstr($G['path']['relative'],'../../')){
			$html = preg_replace('/(=["\'\s]*)\.\.\/(\w)/',"\\1{$G['path']['relative']}\\2",$html);
		}
		return $html;
	}
}
?>